<!DOCTYPE html>
<html>
    <?php
    require_once "../DB/db.php";
    include_once '../class/UserClass.php';
    include_once '../Functions/imageFunctions.php';
    ?>
    <head>
        <title>Nettbutikk</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../CSS/mainFrame.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/general.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
    </head>
    <body>
        <!-- Display the main frame: -->
        <div class="wrapper">
            <?php
            include_once "../MainFrame/header.php";
            include_once "../MainFrame/menuBar.php";
            include_once "../MainFrame/sideBar.php";
            ?>

            <!-- The main search.php code lies within "content": -->
            <div class="content">

                <?php
                if (isset($_GET['search']) && $_GET['search'] != '') {
                    echo '<h1 id="searchHeader" >Resultatet av søket: ' . $_GET['search'] . '</h1>';
                    // Find all the items from search
                    $search_query = getSearchItems();
                    // Loop through all items
                    echo '<table id="searchTable">';
                    echo '<tr><th></th><th>Vare Info</th><th>Antall</th><th>Pris</th><th>Kjøp</th></tr>';
                    while ($row = $search_query->fetch()) {
                        // make the search lowercase, uppercase and first letter is uppercase
                        $temp_item_name_upper = strpos($row['name'], strtoupper($_GET['search']));
                        $temp_item_name_lower = strpos($row['name'], strtolower($_GET['search']));
                        $temp_item_name_first = strpos($row['name'], ucfirst($_GET['search']));
                        if ($temp_item_name_upper === false && $temp_item_name_lower === false && $temp_item_name_first === false) {
                            
                        } else {
                            $item_name = $row['name'];
                            $name_copy = ucwords($item_name);
                            $item_description = substr($row['description'], 0, 50) . "...";
                            $item_image = $row['picture'];
                            $item_image = getImageFromBlob($item_image, 100 ,80);

                            echo '<tr>';
                            echo '<td><a href="item.php?itemId=' . $row['vid'] . '">' . $item_image . '</a></td>';
                            echo '<td><a href="item.php?itemId=' . $row['vid'] . '">' . $name_copy . '</a><br>' . $item_description . '</td>';
                            echo '<form class="searchBuyItem" method="GET" action="/Functions/cookieFunctions.php">';
                            echo '<td><input type = "text" name="antall" value="1" size="2"></td>';
                            echo '<td>' . $row['price'] . ',-</td>';
                            echo '<input name="vareNavn" type="hidden" value="' . $row['vid'] . '">';
                            echo '<td><input name="vareKjop" type="submit" value="Kjop"></td>';
                            echo '</form>';
                            echo '</tr>';
                        }
                    }
                    echo '</table>';
                } else {
                    echo '<h1 id="searchHeader" >Du søkte ikke etter noe!</h1>';
                }
                ?>               
            </div>
            <!-- Display the footer in the main frame. -->
            <?php
            include_once "../MainFrame/footer.php";
            ?>
        </div>
    </body>



    <?php

    function getSearchItems() {
        // Get all the items from search
        global $db;
        $search_query = $db->query('SELECT * FROM item');
        return $search_query;
    }
    ?>
</html>
